package com.scaupos.mis11c.Controller;


import com.scaupos.mis11c.Entities.ClothType;
import com.scaupos.mis11c.Repository.ClothTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
public class ClothTypeController {
    @Autowired
    ClothTypeRepository clothTypeRepository;

    @PostMapping(path = "add")
    public void addClothType(@RequestBody ClothType clothType){
        clothTypeRepository.save(clothType);
    }

    @RequestMapping("/find")
    public ClothType findById(@RequestParam(value="id",required=false)Integer id){
        return clothTypeRepository.findById(id).get();
    }
    @RequestMapping("/findall")
    public List<ClothType> findAll(){
        return clothTypeRepository.findAll();
    }


    @RequestMapping("/clothType/find")
    @ResponseBody
    public ClothType searchClothType(String clothType){
        return clothTypeRepository.findByClothType(clothType);
    }

    @RequestMapping("/update")
    @ResponseBody
    public boolean updateClothType(@RequestParam(value="clothType")String clothType
            ,@RequestParam(value="newClothType")String newClothType) {
        int id = searchClothType(clothType).getId();
        if(searchClothType(clothType)!=null){
            findById(id).setClothType(newClothType);
            clothTypeRepository.save(findById(id));
            return true;
        }
        else
            return false;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public boolean deleteClothType(@RequestParam(value = "clothType") String clothType){
        int id = searchClothType(clothType).getId();
        if(searchClothType(clothType)!=null){
            clothTypeRepository.deleteById(id);
            return true;
        }
        else
            return false;
    }




}
